package com.atguigu.mall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.Collections;

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);                                   // 允许发送 Cookie
        config.addAllowedMethod("*");
        config.setAllowedHeaders(Collections.singletonList("*"));           // 允许所有请求 Header
        config.setAllowedOrigins(Collections.singletonList("*"));           // 允许所有请求来源
//        config.setMaxAge(1800L);                                            // 有效期 1800 秒
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);                                   // 创建 CorsFilter 过滤器
    }

}
